package com.leyou.web;

import com.leyou.entity.CartsItem;
import com.leyou.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("cart")
public class tradeController {

    @Autowired
    private CartService cartService;

    /**
     * 登录加入购物车
     * @param cartsItem
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody CartsItem cartsItem) {
        this.cartService.addCart(cartsItem);
        return ResponseEntity.status(200).build();
    }

    /**
     * 未登录加入购物车
     * @param cartsItem
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Void> localAddCart(@RequestBody List<CartsItem> cartsItem) {
        this.cartService.localAddCart(cartsItem);
        return ResponseEntity.status(200).build();
    }

    /**
     * 增加商品数量
     * @param skuId
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> putCart(
            @RequestParam(value = "id",required = true) Long skuId,
            @RequestParam(value = "num",required = true) Long num ) {
        this.cartService.putCart(skuId, num);
        return ResponseEntity.status(200).build();
    }

    /**
     * 购物车展示
     * @param request
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CartsItem>> listCartsItem(HttpServletRequest request) {

        return ResponseEntity.ok(this.cartService.listCartsItem(request));
    }

}
